package com.changgou.web.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author LQQ
 * @Date 2020/3/11 19:36
 * @Version 1.0
 */

/**
 * 购物车页面渲染
 */
@Controller
@RequestMapping("/wcart")
public class CartController {
    @Autowired
    private OrderFeign orderFeign;
    /**
     * 查询购物车
     *
     * @param model
     * @return
     */
    @GetMapping("/list")
    //页面跳转用String
    public String list(Model model) {
        //map里封装了:购物车总价,总数量,列表数据
        Map map = orderFeign.list();

        //封装到model中
        model.addAttribute("items", map);

        //声明要跳转的页面
        return "cart";

    }


    /**
     * 添加购物车
     *
     * @param id
     * @param num
     * @return
     */
    @GetMapping("/add")
    @ResponseBody
    public Result<Map> add(String id, Integer num) {

        orderFeign.addCart(id, num);

        //添加完购物车得更新购物车的数据
        Map map = orderFeign.list();

        return new Result<>(true, StatusCode.OK, "添加购物车成功", map);
    }
    
}
